<?php
/**
 * 
 * @author hushuai
 *
 */
class DishesAction extends CommonAction{
	
	
	//餐厅所属菜品列表
	public function index(){
		
		$model = D("Dishes");

		//获取餐厅的菜品
		$map['restid'] = $this->_param("id","intval",4);
		$this->assign("outletid",$map['restid']);
		$this->setOutletInfo($map);
		
		//session
		$query = addressJsonDecode();
		foreach($query as $k=>$v){
			$query[$k] = urldecode($v);
		}
		$this->assign("result",$query);
		
		//如果没有选择城市或者没有选择具体地址，那么就要返回去重新选择
		if(empty($query['city'])||empty($query['adds'])){
			$this->assign("noneCity","1");
		}
		
		//获取餐厅菜品
		$json   = sendRest("RMS/Product/dishByCondition" ,$map);
		$list   = $json['data'];
		$this->assign("restid",$map['restid']);
		
		//获取推荐的前10条数据
		$topTen = $model->getTopTen($map);
		$this->assign("topTen",$topTen['data']);
		
		//获取餐厅的菜品分类菜品数量统计
		$array = $model->getCuisineType($topTen['sortid'],$list);
		$this->assign("sort",$array['data']);
		$this->assign("dishSort",$array['dishSort']);
		
		//获取餐厅详情
		$view   = M('Outlet')->find($map['restid']);
		//$view   = sendRest("BDC/Outlet/detail",array("id"=>$map['restid']));
		//poi云检索，获取用户周边一定范围的圆形区域内的餐厅列表数据
		$list   = D("Restaurant")->getOutletArray($query);  
		$detail = $model->getTime($view,$list);
		$this->assign("detail",$detail);
		
		session("dliverytime",$detail['time']);
		session("dliveryfee", $detail['sever']);
		
		//餐厅公告
		$lang    = getLang();
		$service = $lang=='zh-cn'?$detail['service']:$detail['enservice'];
		$notice  = explode("\n",$service);
		$this->assign('notice',$notice);
		
		//获取城市的饮品
		$cityid = $query['cityid'];
		$drink  = $model->getCityDrink($cityid);
		$dsort  = $model->setCityDrink($drink);
		$this->assign("drink",$drink);
		$this->assign('drinkSort',$dsort);
		
		//获取饮品分类数组
		/*foreach ($drink as $k=>$v){
			$typeArr[] = $v['type'];
		}
		
		//获取城市的饮品分类饮品数量统计
		$drinkSort = $model->getDrinkSort($typeArr,$drink);
		$this->assign("drinkSort",$drinkSort);*/
		
		//场景图片
		$cjthumb    = find_thumb($view['solevar'],'cjthumb','Outlet');
		$this->assign("cjthumb",$cjthumb);
		
		$this->display();
	}
	
	/**
	 * 将菜品添加到购物车返回购物车列表
	 */
 	public function cartList(){
 		
 		if(IS_AJAX){
 			
 			//获取用户信息(id和类型)
 			$user = getMember();
 			$_POST['dataid'] = $user['id'];
 			$_POST['type']   = $user['type'];
 			
			if($_POST['isprop']=="haveprop"){
				
				$array = $_POST['flag'];
				$gnfr  = $this->arrray_remove($array);   //移除掉数组中值为空的数组
				
				ksort($gnfr);//对数组进行排序
				foreach ($gnfr as $k=>$v){
					ksort($gnfr[$k]['config']);
				}
				
				//将数组属性拼接成带有等级标识的字符串
				$part = $this->array_level_str($gnfr);
				
				//计算商品和所有属性的价格之和
				foreach ($gnfr as $k=>$v){
					if($v['xtype']==1){
						foreach ($v['config'] as $m=>$n){
							$mprice = $v['config'][$m]['mprice'];
							if($mprice){
								$propPrice += $mprice;
								$psPrice   += $n['psprice'];
							}
						}
					}else{
						foreach ($v['config'] as $m=>$n){
							$propPrice += $n['num']*$n['mprice'];
							$psPrice   += $n['num']*$n['psprice'];
						}
					}
				}
				//获取数组的索引并拼接成字符串
				$array_str = $this->get_index_str($gnfr);
				//将数组转换成json字符串
				$_POST['gnfr']     = $gnfr;
				$_POST['price']    = $propPrice+$_POST['mprice'];
				$_POST['psPrice']  = $psPrice;                     //属性成本价
				$_POST['dprice']   = $_POST['mprice'];
				$_POST['dishflag'] = $_POST['id'].$array_str;
				$_POST['part']     = $part;
				$_POST['flag']     = "dishes";
				
			}else{
				$_POST['gnfr']  = "";
				$_POST['part']  = "";
				$_POST['price'] = $_POST['price'];
				$_POST['psPrice'] = 0.00;
				$_POST['dprice']= $_POST['price'];
				$_POST['flag']  = $_POST['model'];
			}
			$_POST['outletid'] = $_POST['outletid'];
 			$cart  = sendRest("RMS/Cart/addtoCart", $_POST);
 			$this->assign("list",$cart['data']);
 			
 			$content = $this->fetch();
 			$this->ajaxReturn($content);
 		}
	}
	
	/**
	 * 获取数组中的索引并拼成字符串
	 */
	private function get_index_str($array){
		
		
		$str = "";
		foreach ($array as $k=>$v){
			$str.=$k;
			foreach ($v['config'] as $z=>$y){
				$str.=$z;
				foreach($y as $m=>$n){
					if($m=="num"){
					  $str .= $n;
					}
				}
			}
		}
		
		return $str;
	}
	/**
	 * 移除掉数组中值为空的元素
	 */
	private function arrray_remove($array){
		foreach ($array as $k=>$v){
			foreach ($v['config'] as $z=>$y){
				foreach($y as $m=>$n){
					if(empty($n)){
						unset($array[$k]["config"][$z][$m]);
					}
				}
				if(empty($array[$k]["config"][$z])){
					unset($array[$k]["config"][$z]);
				}
				if(empty($array[$k]["config"][$z]['title'])){
					unset($array[$k]["config"][$z]);
				}
			}
			if(empty($array[$k]['config'])){
				unset($array[$k]);
			}
		}
		return $array;
	}
	
	/**
	 * 将数组属性拼接成带有等级标识的字符串
	 */
	function array_level_str($array){
		
		$str    = "";
		$string = "";
		
		foreach ($array as $k=>$v){
			$str = $k.":";
			foreach ($v['config'] as $z=>$y){
				$str .= $z.",";
			}
			$string .= $str."|";
		}
		return $string;
	}
	
	/**
	 * 当前用户购物车列表
	 */
	public function cartview(){
			
		if(IS_AJAX){
			//获取用户信息(id和类型)
			$user = getMember();
			
			$map['dataid']   = $user['id'];
			$map['outletid'] = $this->_param("outletid");
			
			$cart = sendRest("RMS/Cart/view",$map);
			$this->assign("list",$cart['data']);
			
 			$content = $this->fetch();
 			$this->ajaxReturn($content);
		}
	}
	
	//session设置餐厅折扣信息
	private function setOutletInfo($map){
		
		$con['id']          = $map['restid'];
		$outlet             = sendRest("RMS/Outlet/detail" ,$con);
		session("outlet_coupon",$outlet['data']);
	}
	
	/**
	 * 修改菜品数量
	 */
	public function setnum(){
	
		if(IS_AJAX){
			sendRest("RMS/Cart/setnum", $_POST);
		}
	}
	/**
	 * 计算总价
	 */
	public function calculateTotal(){
	
		if(IS_AJAX){
			$user = getMember();
			$map['dataid']   = $user['id'];
			$map['outletid'] = $this->_param("outletid","intval",0);
			$json = sendRest("RMS/Cart/calculateTotal", $map);
			$this->ajaxReturn($json);
		}
	}
	/**
	 * 修改菜品数量
	 */
	public function delone(){
		if(IS_AJAX){
			$user = getMember();
			$map['dataid']   = $user['id'];
			$map['outletid'] = $this->_param("outletid");
			$map['id']       = $this->_param("id","intval",0);
			$json = sendRest("RMS/Cart/del", $map);
			
			$this->assign("list",$json['data']);
			$content = $this->fetch();
			$this->ajaxReturn($content);
			
		}
	}
	
	/**
	 * 菜品附加属性弹出窗口
	 */
	public function tanchu(){
		
		if(IS_AJAX){
			
			$outletid = $this->_get("outletid","intval",0);
			$this->assign("outletid",$outletid);
			
			$map['id'] = $this->_get("dishesid","intval",0);
			$json = sendRest("RMS/Product/dishesDetail",$map);
			$data = $json['data'];
			
			$gnfr = json_decode($data['gnfr'],true);
			$data['gnfr'] = $gnfr;
			
			$this->assign("list",$data);
			$content = $this->fetch();
			$this->assign('price',$this->_get('price'));
			$this->ajaxReturn($content);
		}
	}
	
	
	
}
